package com.lee.auth.security.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lee.common.exception.FailureStatus;
import com.lee.common.vo.ResultVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
 * 用于response统一输出处理
 *
 * @author Lee HN
 * @date 2019/11/11 11:16
 */
@Component
@AllArgsConstructor
public final class ResponseOutput {

    private final ObjectMapper objectMapper;

    public void failure(HttpServletResponse response, FailureStatus failureStatus, String message) throws IOException {
        String json = objectMapper.writeValueAsString(ResultVO.failure(failureStatus, message));
        response.setCharacterEncoding(UTF_8.name());
        response.setContentType(APPLICATION_JSON_VALUE);
        response.getWriter().write(json);
    }

}
